package com.mdlib.helper.ui.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ljlib.core.util.EmptyUtils;
import com.ljlib.help.R;
import com.mdlib.helper.event.MoneyEvent;
import com.mdlib.helper.model.entity.ServiceMoney;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：HQY on 16/11/19 14:16
 * 邮箱：hqy_xz@126.com
 */

public class ServiceMoneyDialogFragment extends DialogFragment {

    private static final String SERVICEMONEY = "servicemoney";

    @Bind(R.id.et_service_money_number)
    EditText mEtServiceMoneyNumber;
    @Bind(R.id.iv_service_money_add)
    ImageView mIvServiceMoneyAdd;
    @Bind(R.id.tv_service_money_number)
    TextView mTvServiceMoneyNumber;
    @Bind(R.id.tv_service_money_submit)
    TextView mTvServiceMoneySubmit;
    @Bind(R.id.iv_service_money_bg)
    ImageView mIvServiceMoneyBg;
    @Bind(R.id.rl_minnetonka_service_money)
    RelativeLayout mRlMinnetonkaServiceMoney;
    @Bind(R.id.ll_service_money_close)
    LinearLayout mLlServiceMoneyClose;
    @Bind(R.id.iv_service_money_subtract)
    ImageView mIvServiceMoneySubtract;

    private ServiceMoney money;


    public static ServiceMoneyDialogFragment newInstance(ServiceMoney serviceMoney) {
        if(serviceMoney==null){
            serviceMoney=new ServiceMoney();
            serviceMoney.setMinServiceMoney(Double.parseDouble("10"));
            serviceMoney.setMxServiceMoney(Double.parseDouble("1000"));
            serviceMoney.setServiceMoneyStep(Double.parseDouble("1"));
        }
        ServiceMoneyDialogFragment fragment = new ServiceMoneyDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(SERVICEMONEY, serviceMoney);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (EmptyUtils.isEmpty(getArguments())) {

            dismiss();
        }
        money = (ServiceMoney) getArguments().getSerializable(SERVICEMONEY);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.Dialog_Share);
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_service_money, null);
        builder.setView(view);
        ButterKnife.bind(this, view);
        init();
        return builder.create();
    }

    @Override
    public void onStart() {
        super.onStart();

        // 设置宽度为屏宽、靠近屏幕底部。
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = Gravity.BOTTOM;
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(wlp);
    }


    public void init() {
        mEtServiceMoneyNumber.setText(money.getMinServiceMoney() + "");
        mTvServiceMoneyNumber.setText("￥" + money.getMinServiceMoney());
        mEtServiceMoneyNumber.addTextChangedListener(new TextWatcher() {

            private CharSequence temp;
            private int editStart;
            private int editEnd;

            @Override
            public void beforeTextChanged(CharSequence s, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence s, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                Double etMoney = Double.valueOf(String.valueOf(s));
                if (etMoney < money.getMinServiceMoney()) {
                    mEtServiceMoneyNumber.setText(money.getMinServiceMoney() + "");
                    mTvServiceMoneyNumber.setText("￥" + money.getMinServiceMoney());
                } else if (etMoney > money.getMxServiceMoney()) {
                    mEtServiceMoneyNumber.setText(money.getMxServiceMoney() + "");
                    mTvServiceMoneyNumber.setText("￥" + money.getMxServiceMoney());
                } else {
                    mTvServiceMoneyNumber.setText("￥" + s);
                }

            }
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    @OnClick({R.id.iv_service_money_subtract, R.id.iv_service_money_add, R.id.tv_service_money_submit, R.id.ll_service_money_close})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_service_money_subtract:
                Double subMoney = Double.valueOf(mEtServiceMoneyNumber.getText().toString());
                mEtServiceMoneyNumber.setText(subMoney - money.getServiceMoneyStep() + "");
                break;
            case R.id.iv_service_money_add:
                Double addMoney = Double.valueOf(mEtServiceMoneyNumber.getText().toString());
                mEtServiceMoneyNumber.setText(addMoney + money.getServiceMoneyStep() + "");
                break;
            case R.id.tv_service_money_submit:
                EventBus.getDefault().post(new MoneyEvent(mTvServiceMoneyNumber.getText().toString()));
                dismiss();
                break;
            case R.id.ll_service_money_close:
                dismiss();
                break;
        }
    }


}
